package com.wnx.job.modules.pms.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.wnx.job.common.api.PageResult;
import com.wnx.job.common.api.QueryPageBean;
import com.wnx.job.common.exception.Asserts;
import com.wnx.job.modules.pms.dto.FavorParam;
import com.wnx.job.modules.pms.mapper.PmsFavorMapper;
import com.wnx.job.modules.pms.model.PmsFavor;
import com.wnx.job.modules.pms.service.PmsFavorService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangnaixing
 * @since 2021-12-17
 */
@Service
public class PmsFavorServiceImpl extends ServiceImpl<PmsFavorMapper, PmsFavor> implements PmsFavorService {

    @Resource
    private PmsFavorMapper favorMapper;

    /**
     * 收藏公司
     * @param favor
     */
    @Override
    public void saveCompany(PmsFavor favor) {
        //校验：一个公司有且只能收藏一次
        long count = this.findFavorCountByCompanyIdAndStuId(favor.getCompanyId(), favor.getStudentId());
        if (count > 0) {
            Asserts.fail("您已经成功收藏该用人单位了！");
        }

        //调用持久层，进行新增收藏职位
        favorMapper.insert(favor);
    }

    /**
     * 收藏职位
     * @param favor
     */
    @Override
    public void savePost(PmsFavor favor) {
        //校验：一个职位有且只能收藏一次
        long count = this.findFavorCountByPostIdAndStuId(favor.getPostId(),favor.getStudentId());
        if (count > 0){Asserts.fail("您已经成功收藏该职位信息了！");}

        //调用持久层，进行新增收藏职位
        favorMapper.insert(favor);

    }

    /**
     * 获取喜欢的公司
     * @param queryPageBean
     * @param studentId
     * @return
     */
    @Override
    public PageResult<FavorParam> getFavorCompany(QueryPageBean queryPageBean, Long studentId) {

        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Map<String, Object> queryParams = queryPageBean.getQueryParams();
        queryParams.put("studentId",studentId);
        List<FavorParam> favorParams =  favorMapper.getFavorCompany(queryParams);
        return PageResult.restPage(favorParams);
    }

    /**
     * 获取喜欢的职位
     * @param queryPageBean
     * @param studentId
     * @return
     */
    @Override
    public PageResult<FavorParam> getFavorPost(QueryPageBean queryPageBean, Long studentId) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Map<String, Object> queryParams = queryPageBean.getQueryParams();
        queryParams.put("studentId",studentId);
        List<FavorParam> favorParams =  favorMapper.getFavorPost(queryParams);
        return PageResult.restPage(favorParams);
    }

    /**
     * 根据公司ID和学生ID查询收藏公司情况
     * @param companyId
     * @param studentId
     * @return
     */
    private long findFavorCountByCompanyIdAndStuId(long companyId,long studentId){
            return favorMapper.selectCount(new QueryWrapper<PmsFavor>().eq("company_id",companyId).eq("student_id",studentId));
    }

    /**
     * 根据公司ID和学生ID查询收藏公司情况
     * @param postId
     * @param studentId
     * @return
     */
    private long findFavorCountByPostIdAndStuId(long postId,long studentId){
        return favorMapper.selectCount(new QueryWrapper<PmsFavor>().eq("post_id",postId).eq("student_id",studentId));
    }




}
